const sqlModule = require('../../common/database/sql');
const models = sqlModule.models;

const classModel = models.class;

function list(param) {
  return new Promise(async (resolve, reject) => {
    try {
      const query = {
        filter: {
          where: {
            status: {
              $in: param.status
            }
          },
        },
        orderBy: [{
          classDate: "asc",
          status: "desc"
        }],
        getCount: true,
        select: {
          $master: true,
          "teacher": true // 关联信息
        },
      };
      if (param.pageSize && param.pageNum) {
        query.pageSize = param.pageSize;
        query.pageNumber = param.pageNum;
      }
      if (param.teacherId) {
        query.filter.where.teacherId = {
          $eq: param.teacherId
        }
      }
      if (param.studentId) {
        query.filter.where.classStudent = {
          $in: [param.studentId]
        }
      }
      if (param.neq_id) {
        query.filter.where._id = {
          $neq: param.neq_id
        }
      }
      if (param.type !== '全部' && param.type) {
        query.filter.where.classType = {
          $in: [param.type]
        };
      };
      //批量状态修改
      if (param.date) {
        query.filter.where.classDate = {
          $lt: param.date
        };
      }
      //日期条件查询
      if (param.classDate) {
        query.filter.where.classDate = {
          $eq: param.classDate
        }
      }
      if (param.limit) {
        query.limit = param.limit;
      };
      const todos = await models.class.list(query);
      resolve(todos.data);
    } catch (error) {
      console.log(error);
      reject(error);
    };
  })
}

//查询用户详情
function byId(id) {
  return new Promise(async (resolve, reject) => {
    try {
      const query = {
        filter: {
          where: {
            _id: {
              $eq: id
            }
          }
        }
      };
      const todo = await classModel.get(query);
      resolve(todo.data);
    } catch (error) {
      reject(error);
    };
  })
}

function add(param) {
  return new Promise(async (resolve, reject) => {
    try {
      const query = {
        data: param,
      };
      const result = await classModel.create(query);
      resolve(result.data);
    } catch (error) {
      reject(error);
    };
  })
}

function edit(param) {
  return new Promise(async (resolve, reject) => {
    try {
      const data = {
        version: (Number(param.version) + 1)
      };
      // 逐一判断并添加
      if (param.classDate != undefined) {
        data.classDate = param.classDate;
      }
      if (param.classType != undefined) {
        data.classType = param.classType;
      }
      if (param.classTime != undefined) {
        data.classTime = param.classTime;
      }
      if (param.teacherId != undefined) {
        data.teacherId = param.teacherId;
      }
      if (param.className != undefined) {
        data.className = param.className;
      }
      // 只有当status存在时才添加到data中
      if (param.status != undefined) {
        data.status = param.status;
      }
      const query = {
        data: data,
        filter: {
          where: {
            _id: {
              $eq: param._id
            },
            version: {
              $eq: param.version
            }
          }
        }
      }
      const result = await classModel.update(query);
      resolve(result.data);
    } catch (error) {
      reject(error);
    };
  })
}

function reservation(param) {
  return new Promise(async (resolve, reject) => {
    try {
      const query = {
        data: {
          classStudent: param.classStudent,
          version: (Number(param.version) + 1)
        },
        filter: {
          where: {
            _id: {
              $eq: param._id
            },
            version: {
              $eq: param.version
            },
            status: {
              $nin: [2, 3, 4]
            }
          }
        }
      }
      const result = await classModel.update(query);
      resolve(result.data);
    } catch (error) {
      reject(error);
    };
  })
}

function cancel(param) {
  return new Promise(async (resolve, reject) => {
    try {
      const query = {
        data: {
          classStudent: param.classStudent,
          version: (Number(param.version) + 1)
        },
        filter: {
          where: {
            _id: {
              $eq: param._id
            },
            version: {
              $eq: param.version
            },
            status: {
              $nin: [2, 3, 4]
            }
          }
        }
      }
      const result = await classModel.update(query);
      resolve(result.data);
    } catch (error) {
      reject(error);
    };
  })
}

module.exports = {
  list,
  byId,
  add,
  edit,
  reservation,
  cancel
}